import Utils.GetFileEncodingByPath;

import java.io.*;
import java.util.HashSet;

public class fileCopy {
    public static void main(String[] args) throws IOException {
//        for(int i = 2006; i< 2020;i++) {
//            String year = String.valueOf(i);
////            int count = 0;
//            File[] f_all = new File("F:\\需要增加的年报\\pdf\\" + year).listFiles();
//            String targetDir = "F:\\2006-2019年报_新版本\\" + year + "年年报\\";
//            for (File f : f_all) {
//                try{
//                    copyFileUsingFileStreams(f,new File(targetDir+f.getName()));
//                }catch (Exception e){
//
//                }
//
//                System.out.println(f.getName());
//            }
//        }
        File[] f_all = new File("D:\\Lights\\全国县级行政区划数据").listFiles();
        HashSet<String> names = txt2Set(new File("xianNames3.txt"));
//        System.out.println(names);
        for (File f:f_all) {
            String name = f.getName().replace("_","");
            int end = name.indexOf(".");
            name = name.substring(0,end);
//            System.out.println(name.substring(0,end));
            if(names.contains(name)){
                System.out.println(name);
                copyFileUsingFileStreams(f,new File("D:\\Lights\\test_0\\"+f.getName()));
            }

        }
    }
    private static void copyFileUsingFileStreams(File source, File dest)
            throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, bytesRead);
            }
        } finally {
            input.close();
            output.close();
        }
    }
    public static HashSet<String>  txt2Set(File file){
        HashSet<String> set = new HashSet<>();
//        StringBuilder content = new StringBuilder();
        try {
            String coding = GetFileEncodingByPath.getEncodingByPath(file.getAbsolutePath());
            InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file),coding);
            BufferedReader bufferedReader = new BufferedReader(streamReader);
            String line;
            while ((line = bufferedReader.readLine())!=null){
                if(line.trim().length() >0){
                    set.add(line.trim());
                }
//                content.append(line);
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        return content.toString();
        return set;
    }
}
